package com.hexb.smh.entity.param.evaluate;

import com.hexb.smh.entity.EvaluateLevel;
import com.hexb.smh.utils.AccountHelper;
import com.hexb.smh.utils.Converters;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import java.math.BigDecimal;

import static com.hexb.smh.utils.SmhConstant.EVALUATE_LEVEL_PATTERN;

/**
 * @author : hexb
 */
@Data
public class EvaluateLevelParam {

    @ApiModelProperty(value = "等级名称", example = "-A")
    @NotNull
    @Size(min = 1, max = 20)
    private String levelName;

    @ApiModelProperty(value = "等级判定起始值(包含)", example = "-10")
    @Pattern(regexp = EVALUATE_LEVEL_PATTERN)
    private String valueFrom;

    @ApiModelProperty(value = "等级判定结束值(不包含)", example = "-20")
    @Pattern(regexp = EVALUATE_LEVEL_PATTERN)
    private String valueTo;

    @ApiModelProperty(value = "是否达标", example = "false")
    @NotNull
    private Boolean passed;

    public EvaluateLevel toEvaluateLevel() {
        EvaluateLevel el = new EvaluateLevel();
        el.setLevelName(levelName);
        el.setPassed(false);

        BigDecimal a = Converters.stringToDecimal(valueFrom);
        BigDecimal b = Converters.stringToDecimal(valueTo);

        if (null != a && null != b) {
            if (a.compareTo(b) < 0) {
                el.setValueFrom(a);
                el.setValueTo(b);
            } else {
                el.setValueFrom(b);
                el.setValueTo(a);
            }
        } else if (a != null) {
            if (a.compareTo(new BigDecimal(0)) < 0) {
                el.setValueTo(a);
            } else {
                el.setValueFrom(a);
            }
        } else if (b != null) {
            if (b.compareTo(new BigDecimal(0)) < 0) {
                el.setValueTo(b);
            } else {
                el.setValueFrom(b);
            }
        }
        el.setCreateBy(AccountHelper.getAccount().getId());
        return el;
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    public static class EvaluateLevelInsertParam extends EvaluateLevelParam {


    }

    @Data
    @EqualsAndHashCode(callSuper = true)
    public static class EvaluateLevelUpdateParam extends EvaluateLevelParam {

        public EvaluateLevel toEvaluateLevel(Integer id) {
            EvaluateLevel el = super.toEvaluateLevel();
            el.setUpdateBy(AccountHelper.getAccount().getId());
            el.setId(id);
            return el;
        }
    }
}
